package com.softeng.network.chess;

public class NetworkCommand
{
	private String ID = null;
	public final static String INITIALIZE = "INIT";
	public final static String MOVE = "MOVE";
	public final static String CHAT = "CHAT";
	
	private String type;
	
	private int currentPosition = -1;
	private int position = -1;
	
	private String color = "";
	
	public NetworkCommand( )
	{

	}
	
	public NetworkCommand( String id )
	{
		ID = id;
	}
	
	public String generateCommand()
	{
		String command = "";
		
		if( ID != null)
		{
			command += "<ID>" + ID + "</ID>";
		}
		
		if(this.type != null)
		{
			command += "<TYPE>" + this.type + "</TYPE>";
		}
		
		if(this.currentPosition != -1)
		{
			command += "<CURPOS>" + this.currentPosition + "</CURPOS>";
		}
		
		if(this.position != -1)
		{
			command += "<POS>" + this.position + "</POS>";
		}
		
		if(!this.color.equals(""))
		{
			command += "<COLOR>" + this.color + "</COLOR>";
		}
		
		return command;			
	}
	
	public boolean parse( String cmd )
	{
		//System.out.println( "Command: " + cmd );
		
		int start = cmd.indexOf( "<ID>" );
		int end = cmd.indexOf( "</ID>" );
		
		if( start != -1 && end != -1 )
		{
			ID = cmd.substring( start + 4, end );
		}
		
		start = -1;
		end = -1;
		
		start = cmd.indexOf( "<TYPE>" );
		end = cmd.indexOf( "</TYPE" );
		
		if( start != -1 && end != -1)
		{
			type = cmd.substring( start + 6, end );
		}
		
		start = -1;
		end = -1;
		
		start = cmd.indexOf( "<CURPOS>" );
		end = cmd.indexOf( "</CURPOS>" );
		
		if( type.equals( "MOVE" ) && start != -1 && end != -1)
		{
			currentPosition = Integer.parseInt( cmd.substring( start + 8, end ) );
		}
		
		start = -1;
		end = -1;
		
		start = cmd.indexOf( "<POS>" );
		end = cmd.indexOf( "</POS>" );
		
		if( type.equals( "MOVE" ) && start != -1 && end != -1 )
		{
			position = Integer.parseInt( cmd.substring( start + 5, end ) );
		}
		
		start = -1;
		end = -1;
		
		start = cmd.indexOf( "<COLOR>" );
		end = cmd.indexOf( "</COLOR>" );
		if( type.equals( "INIT" ) && start != -1 && end != -1 )
		{
			color = cmd.substring(start + 8, end);
		}
		
		return true;
	}
	
	public String getID()
	{
		return ID;
	}
	
	public String getType()
	{
		return type;
	}
	
	public void setType( String t )
	{
		this.type = t;
	}
	
	public void setCurPosition( int i )
	{
		this.currentPosition = i;
	}
	
	public void setColor( String color )
	{
		this.color = color;
	}
	
	public void setPosition( int i )
	{
		this.position = i;
	}
	
	public int getCurPosition()
	{
		return this.currentPosition;
	}
	
	public int getPosition()
	{
		return this.position;
	}
	
	public String getColor()
	{
		return this.color;
	}
}
